﻿using System;
using System.Collections.Generic;
using ZX.Web.UI.Controls;
using ZX.SYS.Library;
using ZX.CRM.Bussiness;
using ZX.CRM.Library;
using ZX.DBModule;
using ZX.Common;
using ZX.BLL.Extend;
using ZX.BLL;
using ZX.Web.UI.Controls.ControlBind;
using ZX.EHR.Library;
using System.Data;

namespace ZX.WebApp.Crm.Sale
{
    public partial class AddOrder : EditUIPage
    {
        BTCrmCustomer bll = new BTCrmCustomer();
        BTCrmCrustomerOrder order = new BTCrmCrustomerOrder();
        BDatPhraseDetail bllDetail = new BDatPhraseDetail();
        BCrmProductPack bllPack = new BCrmProductPack();
        string strFlag = "", strListProduct = "", strUnitId = "", strOrderType = "",
            strListProductType = "", strListOrderProduct = "", strState = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.UserId == null)
            {
                Response.Redirect("../../Error.aspx?message=1&functionId=" + FunctionID);
            }
            if (DataAction == "insert")
            {
                if (PageTitle == "新增一推订单")
                {
                    this.Code.Value = IDMaker.GetSequenceStr("BILL006");
                }
                else if (PageTitle == "新增销售订单" || PageTitle == "新增营销订单" || PageTitle == "新增历史订单")
                {
                    this.Code.Value = IDMaker.GetSequenceStr("BILL001");
                }
                else if (PageTitle == "新增赠送订单")
                {
                    this.Code.Value = IDMaker.GetSequenceStr("BILL004");
                }
                this.WorkFlowInsId.Value = Guid.NewGuid().ToString();
                this.WorkTaskInsId.Value = Guid.NewGuid().ToString();
            }
            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            string id = this.GetStringFromQueryString("id", "");
            BaseEntity instance = ZX.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
            BaseEntity entity = bInstance.GetEntityById(id) as BaseEntity;
            this.EditTemplate.Text = CreateInstanceJsObject(instance, entity);

            #region ------ dropdownlist ------
            //订单类型
            IList<DatPhraseDetail> ListTypeId = bllDetail.GetDatPhraseDetailByClassValue("OrderType");
            DropDownListControl.DropDataSourceBind(this.TypeID, ListTypeId, "PhraseText", "PhraseID");
            //币别
            IList<DatPhraseDetail> ListValuteId = bllDetail.GetDatPhraseDetailByClassValue("ValuteID");
            DropDownListControl.DropDataSourceBind(this.ValutaID, ListValuteId, "PhraseText", "PhraseID");
            //付款方式
            IList<DatPhraseDetail> ListPaymentId = bllDetail.GetDatPhraseDetailByClassValue("PaymentID");
            DropDownListControl.DropDataSourceBind(this.PaymentId, ListPaymentId, "PhraseText", "PhraseID");
            //订单状态
            IList<DatPhraseDetail> ListStateId = bllDetail.GetDatPhraseDetailByClassValue("OrderStateID");
            DropDownListControl.DropDataSourceBind(this.StateID, ListStateId, "PhraseText", "PhraseID");
            //订单期数
            //IList<CrmProductLayout> ListProductLayout = bll.GetCrmProductLayout();
            //DropDownListControl.DropDataSourceBind(this.CourseLoyoutID, ListProductLayout, "CourseLayoutName", "ID");
            //产品包
            IList<CrmProductPack> ListProductPack = bllPack.GetAllProductPackByID();//new Guid("4BA06954-FD31-4F13-8A35-DC86E09D020C")
            DropDownListControl.DropDataSourceBind(this.ProductPackID, ListProductPack, "Packname", "ID");

            //赠送类型
            IList<DatPhraseDetail> ListPresentationType = bllDetail.GetDatPhraseDetailByClassValue("PresentationType");
            DropDownListControl.DropDataSourceBind(this.PresentationType, ListPresentationType, "PhraseText", "PhraseID");
            #endregion

            #region-----------jsonData-----------
            IList<V_CrmProduct> ListProduct = order.GetAllCrmCustomerProduct("");
            if (ListProduct != null && ListProduct.Count > 0)
            {
                IList<BaseEntity> _ListProduct = new List<BaseEntity>();
                foreach (var _e in ListProduct)
                {
                    _ListProduct.Add(_e);
                }
                strListProduct = _ListProduct.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
            }
            //产品类型
            IList<DatPhraseDetail> ListProductType = bllDetail.GetDatPhraseDetailByClassValue("ProductType");
            if (ListProductType != null && ListProductType.Count > 0)
            {
                IList<BaseEntity> _ListProductType = new List<BaseEntity>();
                foreach (var _e in ListProductType)
                {
                    _ListProductType.Add(_e);
                }
                strListProductType = _ListProductType.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
            }
            //单位
            IList<DatPhraseDetail> ListUnitId = bllDetail.GetDatPhraseDetailByClassValue("UnitId");
            if (ListUnitId != null && ListUnitId.Count > 0)
            {
                IList<BaseEntity> _ListUnitId = new List<BaseEntity>();
                foreach (var _e in ListUnitId)
                {
                    _ListUnitId.Add(_e);
                }
                strUnitId = _ListUnitId.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
            }
            //状态
            IList<DatPhraseDetail> ListOrderProductStateId = bllDetail.GetDatPhraseDetailByClassValue("OrderStateID");
            if (ListOrderProductStateId != null && ListOrderProductStateId.Count > 0)
            {
                IList<BaseEntity> _ListOrderProductStateId = new List<BaseEntity>();
                foreach (var _e in ListOrderProductStateId)
                {
                    _ListOrderProductStateId.Add(_e);
                }
                strState = _ListOrderProductStateId.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
            }
            #endregion

            strFlag = this.GetStringFromQueryString("flag", "");
            if (strFlag == "0") //新增
            {
                this.ID.Value = Guid.NewGuid().ToString();
                DataTable dt = order.GetBranchCompany(new Guid(this.UserId.ToString()));
                if (dt != null && dt.Rows.Count > 0)
                {
                    this.DeptId.Value = dt.Rows[0][1].ToString();
                    this.txtDeptId.Value = dt.Rows[0][0].ToString();
                }
            }
            else if (strFlag == "1" || strFlag == "3" || strFlag == "4")  //变更|流程查看(审核) 
            {
                dataBinds(id);
            }
            else if (strFlag == "2") //可以修改所有信息
            {
                this.WorkTaskInsId.Value = this.GetStringFromQueryString("WorktaskInsId", "");
                dataBinds(id);
                #region---------------jsonData---------------
                IList<V_CrmOrderProduct> ListOrderProduct = order.GetAllOrderProduct(new Guid(id));
                if (ListOrderProduct != null && ListOrderProduct.Count > 0)
                {
                    IList<BaseEntity> _ListOrderProduct = new List<BaseEntity>();
                    foreach (var _e in ListOrderProduct)
                    {
                        _ListOrderProduct.Add(_e);
                    }
                    strListOrderProduct = _ListOrderProduct.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                }
                #endregion
            }
        }
        private void dataBinds(string id) 
        {
            this.ID.Value = id;
            this.btnCustomerID.Visible = false;
            V_CrmCustomerInfoOrder OrderInfos = new V_CrmCustomerInfoOrder();
            if (id == "")
            {
                id = this.GetStringFromQueryString("WorkflowInsId", "");
                OrderInfos = order.GetAllCrmCustomerInfoOrderByWorkFlowInsId(new Guid(id));
                if (OrderInfos != null)
                {
                    dataBindCustomerInfoOrder(OrderInfos);
                }
            }
            else
            {
                OrderInfos = order.GetAllCrmCustomerInfoOrder(new Guid(id));
                if (OrderInfos != null)
                {
                    dataBindCustomerInfoOrder(OrderInfos);
                }
            }
            
            //付款方式
            DropDownListControl.DropDownListSelect(this.PaymentId, OrderInfos.PaymentId.ToString());
            //订单类型
            DropDownListControl.DropDownListSelect(this.TypeID, OrderInfos.TypeID.ToString());
            //币别
            DropDownListControl.DropDownListSelect(this.ValutaID, OrderInfos.ValutaID.ToString());
            //订单期数
            //DropDownListControl.DropDownListSelect(this.CourseLoyoutID, OrderInfos.CourseLoyoutID.ToString());
            //订单状态
            DropDownListControl.DropDownListSelect(this.StateID, OrderInfos.StateID.ToString());
            //产品包
            DropDownListControl.DropDownListSelect(this.ProductPackID, OrderInfos.ProductPackID.ToString());
            //赠送类型
            DropDownListControl.DropDownListSelect(this.PresentationType, OrderInfos.PresentationType.ToString());
            #region-----------jsonData-----------
            IList<V_CrmOrderProduct> ListOrderProduct = order.GetAllOrderProduct(OrderInfos.ID);
            if (ListOrderProduct != null && ListOrderProduct.Count > 0)
            {
                IList<BaseEntity> _ListOrderProduct = new List<BaseEntity>();
                foreach (var _e in ListOrderProduct)
                {
                    _ListOrderProduct.Add(_e);
                }
                strListOrderProduct = _ListOrderProduct.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
            }
            #endregion
        }

        private void dataBindCustomerInfoOrder(V_CrmCustomerInfoOrder Orders)
        {
            if (Orders != null)
            {
                //Using UIMapping replace here!
                //ZX.Mapping.IMapping mapper = new ZX.Mapping.WebUiEnMapper(Orders, this);
                //mapper.EN2UI();
                if (Orders.Code.IndexOf("DZ") >= 0) //标识赠送订单
                {
                    strOrderType = "1";
                }
                else
                {
                    strOrderType = "0";
                }
                this.ID.Value = Orders.ID.ToString();
                this.Code.Value = Orders.Code;
                this.CustomerID.Value = Orders.CustomerId.ToString();
                this.Date.Value = Orders.Date.ToString("yyyy-MM-dd");
                this.txtCustomerIDs.Value = Orders.CustomerCode;
                this.CustomerName.Value = Orders.CustomerName;
                this.CustomerType.Value = Orders.TypeName;
                this.PersonId.Value = Orders.PersonName;
                this.TradeId.Value = Orders.TradeName;
                this.CreateDate.Value = Orders.CompanyCreateDate.ToString("yyyy-MM-dd");
                this.CompanyProduct.Value = Orders.CompanyProduct;
                this.DeptId.Value = Orders.DeptId.ToString();
                this.txtDeptId.Value = Orders.OrderDeptName;
                this.TotalMoney.Value = Orders.TotalMoney.ToString(); //总金额
                this.TotalRebate.Value = Orders.EXP1.ToString(); //总折扣率
                this.AddUserName.Value = Orders.AddUserName; //信息专员
                this.SaleadName.Value = Orders.SaleadviserName; //行销顾问
                this.Explain.Value = Orders.Explain;
                this.CourseLoyoutID.Value = Orders.CourseLoyoutID.ToString();
                this.txtCourseLoyoutID.Value = Orders.CourseLayoutName;
                this.PresentationOrderId.Value = Orders.PresentationOrderId.ToString();
                this.txtPresentationOrderId.Value = Orders.PresentationCode;
                this.PresentationReason.Value = Orders.PresentationReason;

                BUser buser = new BUser();
                if (Orders.CheckUser != "")
                {
                    DatUser du = buser.GetDatUserByName(Orders.CheckUser);
                    if (du != null)
                    {
                        this.txtCustomId.Value = du.UName;
                        this.CustomId.Value = du.UserID.ToString();
                    }
                }
                

                IList<V_CrmOrderPerson> orderPerson = order.GetAllCrmOrderPerson(Orders.ID);
                if (orderPerson != null && orderPerson.Count > 0)
                {
                    for (int i = 0; i < orderPerson.Count; i++)
                    {
                        if (orderPerson[i].ClassValue == "CustomerZJ")
                        {
                            this.CustomerQD.Value = orderPerson[i].UserId.ToString();
                            this.txtCustomerQD.Value = orderPerson[i].UserName + "(" + orderPerson[i].EmployeeCode + ")";
                        }
                        else if (orderPerson[i].ClassValue == "CustomerServer")
                        {
                            this.CustomerServer.Value = orderPerson[i].UserId.ToString();
                            this.txtCustomerServer.Value = orderPerson[i].UserName + "(" + orderPerson[i].EmployeeCode + ")";
                        }
                        else if (orderPerson[i].ClassValue == "CustomerFZ")
                        {
                            this.CustomerFZ.Value = orderPerson[i].UserId.ToString();
                            this.txtCustomerFZ.Value = orderPerson[i].UserName + "(" + orderPerson[i].EmployeeCode + ")";
                        }
                        else if (orderPerson[i].ClassValue == "CustomerFDY")
                        {
                            this.CustomerFD.Value = orderPerson[i].UserId.ToString();
                            this.txtCustomerFD.Value = orderPerson[i].UserName + "(" + orderPerson[i].EmployeeCode + ")";
                        }
                        else if (orderPerson[i].ClassValue == "CustomerQDR")
                        {
                            this.CustomerZJ.Value += "@" + orderPerson[i].UserId.ToString() + "︾"
                                                  + orderPerson[i].CommisionScale.ToString() + "︾"
                                                  + orderPerson[i].Explain.ToString() + "︾";
                            this.txtCustomerZJ.Value += orderPerson[i].UserName + "(" + orderPerson[i].EmployeeCode + ")" + "(" + orderPerson[i].CommisionScale + "%)" + ",";
                        }
                    }
                }
            }
        }

        #region-----------
        /// <summary>
        /// 标题
        /// </summary>
        public string PageTitle
        { get { return this.GetStringFromQueryString("title", ""); } }
        /// <summary>
        /// 状态
        /// </summary>
        public string Flag
        { get { return strFlag; } }
        /// <summary>
        /// 产品列表
        /// </summary>
        public string ListCrmProduct
        { get { return strListProduct; } }
        /// <summary>
        /// 产品类型
        /// </summary>
        public string ListProductType
        { get { return strListProductType; } }
        /// <summary>
        /// 计量单位
        /// </summary>
        public string ListUnitId
        { get { return strUnitId; } }
        /// <summary>
        /// 订单产品列表
        /// </summary>
        public string ListOrderProduct
        { get { return strListOrderProduct; } }
        /// <summary>
        /// 当前用户名
        /// </summary>
        public string UName
        {
            get { return this.UserName; }
        }
        /// <summary>
        /// 状态
        /// </summary>
        public string ListState
        { 
            get { return this.strState; } 
        }
        /// <summary>
        /// 0:销售订单  1:赠送订单
        /// </summary>
        public string OrderType
        {
            get { return this.strOrderType; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string strWorkFlowId
        {
            get
            {
                string wtid = this.GetStringFromQueryString("WorkflowId", "");
                if (wtid == "")
                {
                    wtid = this.GetStringFromQueryString("wfid","");
                }
                return wtid;
            } 
        }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkTaskId
        { 
            get
            {
                string wfid = this.GetStringFromQueryString("WorktaskId", "");    
                if(wfid =="")
                {
                    wfid = this.GetStringFromQueryString("wtid","");
                }
                return wfid;
            }
            
        }
        /// <summary>
        /// 
        /// </summary>
        public string strOperatorInstanceId
        { get { return this.GetStringFromQueryString("OperatorInstanceId", ""); } }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkflowInsId
        { get { return this.GetStringFromQueryString("WorkflowInsId", ""); } }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkTaskInsId
        { get { return this.GetStringFromQueryString("WorktaskInsId", ""); } }

        public string strView
        { get { return this.GetStringFromQueryString("view",""); } }
        public string strOperStatus
        { get { return this.GetStringFromQueryString("OperStatus", ""); } }
        public string strDescript
        { get { return this.GetStringFromQueryString("descript", ""); } }
        #endregion
    }
}